package ij.umasuo.gui.working;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JPanel;
import javax.swing.JToggleButton;

/**
 * the working viewer for viewer the 
 * @author storm
 * @date 2013-08-22
 */
public class WorkingViewer extends JPanel implements ActionListener, ComponentListener,ItemListener{

	private static final long serialVersionUID = 1L;
	private static WorkingViewer instance = null;
	
	private JPanel northPanel = new JPanel();
	private JPanel southPanel = new JPanel();
	private JPanel centerPanel = new JPanel();
	private MyImageCanvas canvas = null;
	private Map<Integer,JToggleButton> buttons = new HashMap<Integer,JToggleButton>();
	private Map<Integer,Image> images = new HashMap<Integer,Image>();
	private List<Integer> channels = new ArrayList<Integer>();
	
	private int curChannel = -1;
	
	public WorkingViewer(){
		this.addComponentListener(this);
		setLayout(null);
		this.setSize(450, 290);
		
		canvas = new MyImageCanvas();
		centerPanel.setLayout(new BorderLayout(0, 0));
		centerPanel.add(canvas,BorderLayout.CENTER);
		centerPanel.setBounds(20, 30, 410, 210);
		this.add(centerPanel);
		
		northPanel.setBounds(0, 0, 450, 30);
		this.add(northPanel);
		
		southPanel.setBounds(0, 240, 450, 50);
		southPanel.setLayout(null);
		this.add(southPanel);
		
	}

	/**
	 * reset the working viewer
	 */
	public void reset(){
		channels.clear();
		images.clear();
		buttons.clear();
		curChannel = -1;
		this.repaint();
	}
	
	/**
	 * update the canvas
	 * @param channel
	 * @param image
	 */
	public void updataWorkingCanvas(int channel,Image image){
		if(images.containsKey(new Integer(channel))){
			images.put(new Integer(channel), image);
		}else{
			images.put(new Integer(channel), image);
			JToggleButton tmp = new JToggleButton("Channel" + channel);
			tmp.addItemListener(this);
			channels.add(new Integer(channel));
			buttons.put(new Integer(channel), tmp);
			southPanel.add(tmp);
		}
		if(channel == curChannel){
			canvas.setImage(image);
//			canvas.drawImage(image);
		}else if(curChannel == -1){
			curChannel = channel;
			canvas.setImage(image);
		}
		componentResized(null);//reorder the component
	}
	
	/**
	 * get the instance
	 * @return 
	 */
	public static WorkingViewer getInstance(){
		if(instance == null){
			instance = new WorkingViewer();
		}
		return instance;
	}

	/**
	 * to response the button click
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		
	}

	
	@Override
	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void componentResized(ComponentEvent arg0) {
		// here we reorder the panel
		int height = this.getHeight();
		int width = this.getWidth();
//		System.out.println(width + "  o " + height);
		northPanel.setBounds(0, 0, width, 30);
		
		southPanel.setBounds(0, height-60, width, 60);
		int startX = 10;
		for(int i=0;i<channels.size();i++){
			int chan = channels.get(i);
			JToggleButton tmp = buttons.get(new Integer(chan));
			tmp.setBounds(startX, 10, 100, 25);
			if(curChannel == chan){
				tmp.setSelected(true);
			}else{
				tmp.setSelected(false);
			}
		}
		
		height = height - 90;
		int gapH = 0,gapV = 0;
		if(width>height){
			gapH = width - height;
			centerPanel.setBounds(gapH/2, 30, width-gapH, height);
		}else{
			gapV = height - width;
			centerPanel.setBounds(0, 30+gapV/2, width, height-gapV);
		}
		if(canvas!=null){
			canvas.repaint();
		}
//		this.repaint();
	}

	@Override
	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub
		
	}


	@Override
	public void itemStateChanged(ItemEvent e) {
		JToggleButton tmp = (JToggleButton)e.getSource();
//		System.out.println(tmp.isSelected());
		
	}
}
